package com.evl.ticketRemainder.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.evl.api.ticket.service.ITicketService;
import com.evl.common.constant.factory.PageFactory;
import com.evl.common.controller.BaseController;
import com.evl.common.persistence.model.ScheduleStation;
import com.evl.common.persistence.model.TicketRemainder;
import com.evl.core.vo.BaseResultVo;
import com.evl.core.vo.JsonReturnHelper;
import com.evl.ticketRemainder.service.ITicketRemainderService;
import com.evl.ticketRemainder.vo.TrainSeatScheduleVo;
import com.evl.utils.JsonUtils;
import com.evl.vehicleSchedule.dao.TicketRemainderDao;

/**
 * 余票管理控制器
 *
 * @author 
 * @Date 2017-06-21 15:31:32
 */
@Controller
@RequestMapping("/ticketRemainder")
public class TicketRemainderController extends BaseController {

    private String PREFIX = "/ticketRemainder/";

    @Resource
    TicketRemainderDao ticketRemainderDao;
    
    @Resource
    ITicketRemainderService ticketRemainderService;
    
    @Resource
    ITicketService ticketService;
    

    /**
     * 跳转到余票管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "ticketRemainder.html";
    }

    /**
     * 跳转到添加余票管理
     */
    @RequestMapping("/ticketRemainder_add")
    public String ticketRemainderAdd() {
        return PREFIX + "ticketRemainder_add.html";
    }

    /**
     * 跳转到修改余票管理
     */
    @RequestMapping("/ticketRemainder_update/{ticketRemainderId}")
    public String ticketRemainderUpdate(@PathVariable Integer ticketRemainderId, Model model) {
    	return PREFIX + "ticketRemainder_update.html";
    }

    /**
     * 获取余票管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String departure,@RequestParam(required = false) String arrive,@RequestParam(required = false) String date) {
    	
    	Page<TicketRemainder> page = new PageFactory<TicketRemainder>().defaultPage();
        List<TicketRemainder> result =  ticketRemainderService.selectTicketRemaindersPage(page, departure,arrive,date);
        //page.setRecords(result);
        //return super.packForBT(page);
        return  result;

    }

    /**
     * 新增余票管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add() {
        return super.SUCCESS_TIP;
    }

    /**
     * 锁定座位
     */
    @RequestMapping(value = "/lock")
    @ResponseBody
    public BaseResultVo lockSeats(@RequestParam String trainSeatScheduleIds,@RequestParam Integer ticketRemainderId) {
    	if(StringUtils.isNotBlank(trainSeatScheduleIds)){
    		String [] ids = new String[]{};
    		ids = (String[]) JsonUtils.fromJson(trainSeatScheduleIds, ids.getClass());
    		ticketRemainderService.lockSeats(ticketRemainderId,ids);
    	}
    	
    	
        return JsonReturnHelper.getSuccessWithNull();
    }

    /**
     * 取消锁定座位
     */
    @RequestMapping(value = "/canclelock")
    @ResponseBody
    public BaseResultVo canclelockSeats(@RequestParam String trainSeatScheduleIds,@RequestParam Integer ticketRemainderId) {
    	if(StringUtils.isNotBlank(trainSeatScheduleIds)){
    		String [] ids = new String[]{};
    		ids = (String[]) JsonUtils.fromJson(trainSeatScheduleIds, ids.getClass());
    		ticketRemainderService.canclelockSeats(ticketRemainderId,ids);
    	}
    	
    	
        return JsonReturnHelper.getSuccessWithNull();
    }


    /**
     * 修改余票管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update() {
        return super.SUCCESS_TIP;
    }
    
    /**
     * 跳转到余票详情-座位列表页面
     */
    @RequestMapping("/ticketRemainder_seat_detail/{ticketRemainderId}")
    public String seatDetailPage(@PathVariable Integer ticketRemainderId, Model model) {
    	
    	TicketRemainder tr = ticketRemainderService.selectTicketById(ticketRemainderId);
    	model.addAttribute("ticketRemainder", tr);
    	
        return "/trainSeatSchedule/trainSeatSchedule.html";
    }
    
    /**
     * 跳转到余票详情-线路列表页面
     */
    @RequestMapping("/ticketRemainder_station_detail/{ticketRemainderId}")
    public String stationDetailPage(@PathVariable Integer ticketRemainderId, Model model) {
    	
    	TicketRemainder tr = ticketRemainderService.selectTicketById(ticketRemainderId);
    	model.addAttribute("ticketRemainder", tr);
    	
        return "/scheduleStation/scheduleStation.html";
    }
    
    /**
     * 余票管理详情--座位详情
     */
    @RequestMapping(value = "/seatDetail/list/{ticketRemainderId}")
    @ResponseBody
    public Object seatDetail(@PathVariable Integer ticketRemainderId,@RequestParam(required = false) String carriageNo,@RequestParam(required = false) String seatNo,@RequestParam(required = false) String seatType) {
    	TicketRemainder tr = ticketRemainderService.selectTicketById(ticketRemainderId);
    	
		List<TrainSeatScheduleVo> trainSeatSchedules= ticketRemainderService.getTrainSeatSchedules(tr,carriageNo,seatNo,seatType);
		
		return trainSeatSchedules;
    }
    
    /**
     * 余票管理详情--线路详情
     */
    @RequestMapping(value = "/stationDetail/list/{ticketRemainderId}")
    @ResponseBody
    public Object stationDetail(@PathVariable Integer ticketRemainderId) {
    	TicketRemainder tr = ticketRemainderService.selectTicketById(ticketRemainderId);
    	
		List<ScheduleStation> scheduleStations= ticketRemainderService.getScheduleStations(tr);
		
		return scheduleStations;
    }
}
